package com.chenyun.system.controller;

import com.chenyun.system.model.SysConfig;
import com.chenyun.system.service.ISysConfigService;
import com.chenyun.boot.web.controller.BaseController;
import com.chenyun.common.constants.UserConstants;
import com.chenyun.common.vo.AjaxResult;
import com.chenyun.common.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/config")
@Api(tags = "参数配置-admin")
public class SysConfigController extends BaseController<ISysConfigService,SysConfig>
{
    @Autowired
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @ApiOperation("获取参数配置列表")
    @GetMapping(value = "/list")
    public PageResult list(SysConfig config)
    {
        List<SysConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }



    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    @ApiOperation("根据参数编号获取详细信息")
    public AjaxResult getInfo(@PathVariable Long configId)
    {
        return AjaxResult.SUCCESS(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @ApiOperation("根据参数键名查询参数值")
    @GetMapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey)
    {
        return AjaxResult.SUCCESS(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @ApiOperation("新增参数配置")
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult add(@Validated @RequestBody SysConfig config)
    {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return AjaxResult.FAIL("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }

        return toAjax(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @ApiOperation("修改参数配置")
    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult edit(@Validated @RequestBody SysConfig config)
    {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return AjaxResult.FAIL("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
//        config.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @ApiOperation("删除参数配置")
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        configService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @ApiOperation("刷新参数缓存")
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        configService.resetConfigCache();
        return AjaxResult.SUCCESS();
    }
}
